/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Xadrez.Modelo;

/**
 *
 * @author Jéssica
 */
public class Board implements Cloneable {

    private Square[][] grid;

    public Board() {
        grid = new Square[8][8];

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                grid[i][j] = new Square(i, j);

            }
        }
    }

    public Square[][] getGrid() {
        return this.grid;
    }

    public void resetPositions() {
        this.getGrid()[0][0].setPiece(new Rook("Black"));
        this.getGrid()[7][0].setPiece(new Rook("Black"));
        this.getGrid()[1][0].setPiece(new Knight("Black"));
        this.getGrid()[6][0].setPiece(new Knight("Black"));
        this.getGrid()[2][0].setPiece(new Bishop("Black"));
        this.getGrid()[5][0].setPiece(new Bishop("Black"));
        this.getGrid()[3][0].setPiece(new Queen("Black"));
        this.getGrid()[4][0].setPiece(new King("Black"));
        for (int i = 0; i < 8; i++) {
            this.getGrid()[i][1].setPiece(new Pawn("Black"));
        }


        this.getGrid()[0][7].setPiece(new Rook("White"));
        this.getGrid()[7][7].setPiece(new Rook("White"));
        this.getGrid()[1][7].setPiece(new Knight("White"));
        this.getGrid()[6][7].setPiece(new Knight("White"));
        this.getGrid()[2][7].setPiece(new Bishop("White"));
        this.getGrid()[5][7].setPiece(new Bishop("White"));
        this.getGrid()[3][7].setPiece(new Queen("White"));
        this.getGrid()[4][7].setPiece(new King("White"));
        for (int i = 0; i < 8; i++) {
            this.getGrid()[i][6].setPiece(new Pawn("White"));
        }


    }

    @Override
    public Object clone() {
        try {
            Board outra = (Board) super.clone();
            //Aqui você incializa as suas propriedades.
            return outra;
        } catch (CloneNotSupportedException e) {
            return null; // nunca chamado
        }
    }

    public Board backup(){

        Board copia = new Board();

        for(int i=0; i <= 7; i++){
            for(int j=0; j <= 7; j++){

                if(this.getGrid()[i][j].getPiece() !=null){
                 //   copia.getGrid()[i][j].setPiece((Piece) this.getGrid()[i][j].getPiece().clone());
                       copia.getGrid()[i][j].setPiece((Piece) this.getGrid()[i][j].getPiece());

                }
           
            }

        }

        return copia;
    }
}
